package com.example.myblog.service.impl;

import com.example.myblog.dao.ArticleDao;
import com.example.myblog.pojo.Article;
import com.example.myblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;

/**
 * @BelongsProject: MyBlog
 * @BelongsPackage: com.example.myblog.service.impl
 * @ClassName ArticleServiceImpl
 * @CreateTime: 2025-05-31  21:30
 * @Author: TangLiFei
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleDao articleDao;

    @Override
    public List<Article> findTop6ArticlesByCommentCount() {
        return articleDao.findTop6ArticlesByCommentCount();
    }
    @Override
    public List<Article> findAll() {
        List<Article> articles=articleDao.findAll();
        return articles;
    }

    @Override
    public Article findById(int id) {
        Article article= articleDao.findById(id);
        return article;
    }

    @Override
    public void add(Article article) {
        // 业务逻辑：设置创建时间
        article.setCreateTime(new java.util.Date());
        articleDao.add(article);
    }

    @Override
    public void update(Article article) {
        // 业务逻辑：更新时刷新修改时间
        article.setCreateTime(new java.util.Date());
        articleDao.update(article);
    }

    @Override
    public PageInfo<Article> findAllByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> articles = articleDao.findAll();
        return new PageInfo<>(articles);
    }

    @Override
    public void delete(int id) {
        articleDao.delete(id);
    }

    @Override
    public List<Article> search(String keyword) {
        return articleDao.search(keyword);
    }
}
